package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.config.JwtUtils;
import com.ccc.demoboot.pojo.Code;
import com.ccc.demoboot.pojo.Material;
import com.ccc.demoboot.pojo.Materialstor;
import com.ccc.demoboot.service.CodeService;
import com.ccc.demoboot.service.MaterialService;
import com.ccc.demoboot.service.MaterialstorService;
import com.ccc.demoboot.service.StorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

/**
 * @Author wz
 * @Date 2024/6/28 10:27
 * @PackageName:com.ccc.demoboot.dao
 * @ClassName: MaterialstorContrller
 */
@RequestMapping("/materialstor")
@RestController
public class MaterialstorContrller {
    @Autowired
    MaterialstorService materialstorService;

    @Autowired
    MaterialService materialService;

    @Autowired
    CodeService codeService;

    /**
     * 物资入库申请
     *
     * @param materialstor 入库申请信息
     * @param jwttoken     用户令牌
     * @return 入库申请结果
     */
    @PostMapping("/add")
    public R add(@RequestBody Materialstor materialstor, @RequestHeader("jwttoken") String jwttoken) {
        // 设置申请用户ID和申请时间
        Integer jwttoken1 = JwtUtils.getJwtToken(jwttoken, "id");
        materialstor.setApplyuserid(jwttoken1);
        materialstor.setApplicationtime(new Date());
        materialstor.setStatus(2);
        boolean save = this.materialstorService.save(materialstor);
        return new R().add("save", save);
    }



    /**
     * 查询入库申请
     * 该方法通过POST请求查询与条件匹配的入库申请信息，并返回分页的申请列表
     * 使用了JWT进行用户身份验证，从请求头中解析token以获取用户ID
     *
     * @param map 包含查询条件的Map对象，包括状态、页码、每页条数等
     * @param jwttoken 请求头中的JWT token，用于身份验证
     * @return R对象，包含查询结果，结果以分页形式返回
     */
    @PostMapping("/selectMaterialstor")
    public R selectMaterialstor(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String jwttoken) {
        // 从JWT token中获取当前登录用户ID
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");
        // 从请求参数中获取订单状态
        int status = Integer.valueOf(map.get("status").toString());
        // 从请求参数中获取当前页码
        int page = (int) map.get("page");
        // 从请求参数中获取每页条数
        int pageSize = (int) map.get("pageSize");
        // 从请求参数中获取物料编码
        String codematerial = (String) map.get("codematerial");
        // 从请求参数中获取物料名称
        String materialname = (String) map.get("materialname");
        // 创建查询包装器，并根据物料名称和物料编码进行模糊查询
        QueryWrapper<Map> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("materialname", materialname);
        queryWrapper.like("a.codematerial", codematerial);
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        // 调用服务方法查询入库申请信息
        List<Map<String, Object>> materialstorList = materialstorService.selectAllMaterialstor(queryWrapper,status,applyuserid);
        // 将查询结果转换为分页信息
        PageInfo<Map<String, Object>> list = new PageInfo<>(materialstorList);
        // 将分页信息添加到返回对象中，并返回
        return new R().add("list", list);
    }





    /**
     * 分页查询所有信息
     * @param page
     * @return
     */
    @PostMapping("/selectM/{page}")
    public R selectM(@PathVariable("page") int page){
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Materialstor> list = this.materialstorService.selectMareList();
        PageInfo<Materialstor> materialPageInfo = new PageInfo<>(list);
        return new R().add("materialPageInfo",materialPageInfo);
    }

    /**
     * 根据id查询申请详情
     * @param map
     * @return
     */
    @PostMapping("/selectOneMaterialstor")
    public R selectOneMaterialstor(@RequestBody Map<String, Object> map) {
        int id = (int) map.get("id");
        Map<String, Object> one = materialstorService.selectOne(id);
        return new R().add("one", one);
    }



    

    /**
     * 修改订单状态
     * 0表示不通过，1表示通过，3表示取消
     *
     * @param mapOne 包含订单信息的Map对象
     * @param jwttoken 用户的JWT令牌，用于验证用户身份
     * @return 返回一个R对象，包含操作结果和消息
     */
    @PostMapping("/upMaterialstor")
    public R upMaterialstor(@RequestBody Map<String, Object> mapOne, @RequestHeader("jwttoken") String jwttoken) {
        // 获取当前登录用户ID
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");
        // 从mapOne中获取包含具体订单信息的map
        Map<String, Object> map = (Map<String, Object>) mapOne.get("map");
        // 获取订单状态
        int status = Integer.valueOf(map.get("status").toString());
        // 获取要修改的订单ID
        int id = Integer.valueOf(map.get("id").toString());
        // 获取订单中的材料编码
        String codematerial = map.get("codematerial").toString();
        // 获取订单中的数量
        int content = Integer.valueOf(map.get("content").toString());
        // 获取订单中的仓库ID
        int warehouseid = Integer.valueOf(map.get("warehouseid").toString());
        // 获取订单中的区域ID
        int areaid = Integer.valueOf(map.get("areaid").toString());
        // 获取订单中的货架ID
        int shelvesid = Integer.valueOf(map.get("shelvesid").toString());
        // 创建Materialstor对象并设置相关属性
        Materialstor materialstor = new Materialstor();
        materialstor.setId(id);
        materialstor.setStatus(status);
        materialstor.setThroughtime(new Date());
        boolean b;
        // 如果订单状态不是取消
        if (status != 3) {
            // 设置审核人ID为当前登录用户ID
            materialstor.setReviewerid(applyuserid);
            // 调用updateById方法更新订单状态
            b = materialstorService.updateById(materialstor);
            // 如果订单状态为通过且更新成功
            if (status == 1 && b==true) {
                // 根据材料编码获取Code对象
                Code one = codeService.getCode(codematerial);
                // 更新库存数量
                materialService.upQuantity(one.getId(), warehouseid, areaid, shelvesid, content);
                // 返回审核通过的消息
                return new R().setCode(502).setMessage("审核通过");
            }
        } else {
            // 如果订单状态为取消，返回取消成功的消息
            return new R().setCode(400).setMessage("取消成功");
        }
        // 如果以上条件都不满足，返回系统出错的消息
        return new R().setCode(500).setMessage("系统出错，请联系管理员");
    }

    /**
     * uniAPP查询所有物资审核状态
     * @param map
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniSelectMaterialstor")
    public R uniSelectMaterialstor(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        int status = Integer.valueOf(map.get("status").toString());//获取订单状态
        List<Map<String, Object>> materialstorList = materialstorService.uniSelectAllMaterialstor(status,applyuserid);
        PageInfo<Map<String, Object>> list = new PageInfo<>(materialstorList);
        return new R().add("list", list);
    }

    /**
     * uniAPP物资申购
     * @param maps
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniAdd")
    public R uniAdd(@RequestBody Map<String,Object> maps, @RequestHeader("jwttoken") String jwttoken) {
        Map<String,Object> map = (Map<String, Object>) maps.get("map");
        String codematerial = (String) map.get("codematerial");
        int warehouseid = Integer.valueOf(map.get("warehouseid").toString());
        int areaid = Integer.valueOf(map.get("areaid").toString());
        int shelvesid = Integer.valueOf(map.get("shelvesid").toString());
        int content = Integer.valueOf(map.get("content").toString());
        String remarks = (String) map.get("remarks");
        // 设置申请用户ID和申请时间
        Materialstor materialstor=new Materialstor();
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");
        materialstor.setCodematerial(codematerial);
        materialstor.setWarehouseid(warehouseid);
        materialstor.setAreaid(areaid);
        materialstor.setShelvesid(shelvesid);
        materialstor.setContent(content);
        materialstor.setRemarks(remarks);
        materialstor.setApplyuserid(applyuserid);
        materialstor.setApplicationtime(new Date());
        materialstor.setStatus(2);
        boolean save = this.materialstorService.save(materialstor);
        return new R().add("save", save);
    }

    /**
     * uniAPP查询物资审核详情
     * @param map
     * @return
     */
    @PostMapping("/uniSelectOneMaterialstor")
    public R uniSelectOneMaterialstor(@RequestBody Map<String, Object> map) {
        int id = (int) map.get("id");
        Map<String, Object> one = materialstorService.selectOne(id);
        return new R().add("one", one);
    }

    /**
     * uniAPP修改物资申购审核状态，0不通过 1通过 3取消
     *
     * @param mapOne
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniUpMaterialstor")
    public R uniUpMaterialstor(@RequestBody Map<String, Object> mapOne, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        Map<String, Object> map = (Map<String, Object>) mapOne.get("map");
        int status = Integer.valueOf(mapOne.get("status").toString());//获取订单状态
        int id = Integer.valueOf(map.get("id").toString());//获取要修改的订单id
        String codematerial = map.get("codematerial").toString();
        int content = Integer.valueOf(map.get("content").toString());
        int warehouseid = Integer.valueOf(map.get("warehouseid").toString());
        int areaid = Integer.valueOf(map.get("areaid").toString());
        int shelvesid = Integer.valueOf(map.get("shelvesid").toString());
        Materialstor materialstor = new Materialstor();
        materialstor.setId(id);
        materialstor.setStatus(status);
        materialstor.setThroughtime(new Date());
        boolean b;
        if (status != 3) {
            materialstor.setReviewerid(applyuserid);
            b = materialstorService.updateById(materialstor);
            if (status == 1 && b==true) {
                Code one = codeService.getCode(codematerial);
                materialService.upQuantity(one.getId(), warehouseid, areaid, shelvesid, content);
                //订单审核通过
                return new R().setCode(502).setMessage("审核通过");
            }
        } else {
            //取消订单成功
            return new R().setCode(400).setMessage("取消成功");
        }
        //系统出错
        return new R().setCode(500).setMessage("系统出错，请联系管理员");
    }


    /**
     * 柱状图获取物资数量信息
     * @return
     */ @PostMapping("/zhuSelectQ")
    public R zhuSelectQ(){
        List<Material> list= this.materialService.zhuSelectQ();
        return new R().add("list",list);
    }




    /**
     * 柱状图获取物资名称
     * @return
     */ @PostMapping("/zhuSelectName")
    public R zhuSelectName(){
        List<Material> list= this.materialService.zhuSelectName();
        return new R().add("list",list);
    }


}
